<template>
  <div class="permissionResourceManagement">
    <h3>权限资源管理</h3>
    <div class="list">
      <el-table
        border
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
      >
        <el-table-column type="index" width="50" header-align="center" align="center" label="序号"></el-table-column>
        <el-table-column header-align="center" align="center" prop="id" label="资源id"></el-table-column>
        <el-table-column header-align="center" align="center" prop="parent_id" label="父级id"></el-table-column>
        <el-table-column header-align="center" align="center" prop="operate" label="操作名"></el-table-column>
        <el-table-column header-align="center" align="center" prop="squad" label="排序"></el-table-column>
        <el-table-column header-align="center" align="center" prop="name" label="名称"></el-table-column>
        <el-table-column header-align="center" align="center" label="操作" width="230" fixed="right">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="success"
              @click="handleDelete(scope.$index, scope.row)"
              v-if="permissions_obj.id_51||permissions_obj.id_all"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="currentPage1"
        :page-sizes="[10,30,50,100]"
        :page-size="pageSize"
        layout="total, prev, pager, next,sizes,jumper"
        :total="totalSize"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
export default {
  name: "permissionResourceManagement",
  data() {
    return {
      tableData: [],
      currentPage1: 1,
      pageSize: 10,
      totalSize: 0,
      //根据账号权限判断是否显示删除按钮
      isShowDeleteBtn: false,
      //初始化列表数据
      listData: {},
      // 权限管理
       permissions:[],
      permissions_obj:{},
    };
  },
  created(){ 
    //权限判断
    this.permissions = this.$store.state.permissions;
    let oneOff=false;
    this.permissions.forEach((item, index) => {
      if(item==-1){
        oneOff=true;
      }else{
        this.permissions_obj['id_'+item]=item;
      }
    });
    if(oneOff){
      this.permissions_obj.id_all=true;
    }else{
      this.permissions_obj.id_all=false;
    };
  },
  mounted() {
    //判断权限
    let permissions = this.$store.state.permissions;
    permissions.forEach((item, index) => {
      if (item == -1 || item == 35) {
        this.isShowDeleteBtn = true;
      }
    });
    this.getDataList();
  },
  methods: {
    //获取初始化数据列表
    async getDataList() {
      let parames = {
        handshake: "",
        data: {
          page: this.currentPage1,
          page_size: this.pageSize
        }
      };
      let res = await this.$axios.post(
        this.$api.permissionResourceLists,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.listData = res.data;
        this.totalSize = this.listData.total;
        this.tableData = this.listData.data;
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    },
    // 分页
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage1 = val;
      this.getDataList();
    },
    handleSizeChange(val){
      this.pageSize=val;
      this.getDataList();
    },
    //删除
    handleDelete(index, row) {
      this.$confirm("此操作将永久删除该行数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.deleteMethod(row.id);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    //删除接口
    async deleteMethod(_id) {
      let parames = {
        handshake: "",
        data: {
          id: _id
        }
      };
      let res = await this.$axios.post(
        this.$api.permissionResourceDelete,
        JSON.stringify(parames)
      );
      if (res.code == 200) {
        this.$message({
          message: "删除成功!",
          type: "success"
        });
        this.getDataList();
      } else {
        this.$message({
          message: res.message,
          type: "warning"
        });
      }
    }
  }
};
</script>
<style scoped>
h3 {
  padding: 0.2rem;
  color: #3c763d;
}
.search {
  display: flex;
  margin-top: 0.2rem;
  padding-left: 0.2rem;
  width: 100%;
  flex-wrap: wrap;
}
.el-input {
  width: 2.5rem;
}
label {
  width: 0.83rem;
  height: 0.4rem;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #f5f7fa;
  color: #909399;
  border: 1px solid #dcdfe6;
  border-top-left-radius: 4px;
  border-bottom-left-radius: 4px;
  font-size: 14px;
}
.cond {
  display: flex;
  margin-right: 0.4rem;
  margin-bottom: 0.1rem;
}
.search .el-select >>> .el-input__inner {
  border-left: none;
  border-top-left-radius: 0px;
  border-bottom-left-radius: 0px;
}
.search .el-select {
  width: 1.67rem;
}
.opration {
  margin-top: 0.2rem;
  padding-left: 0.2rem;
}
.page {
  margin-top: 0.2rem;
  display: flex;
  justify-content: center;
}
.list {
  margin-top: 0.2rem;
  padding: 0 0.1rem;
  font-size: 0.12rem;
}
.dialogBtn {
  display: flex;
  justify-content: center;
  margin-top: 0.3rem;
}
.row {
  margin-bottom: 0.1rem;
  display: flex;
  align-items: center;
}
.row span {
  width: 1rem;
  display: block;
}
.el-dialog .el-input {
  width: 80%;
}
.el-dialog .el-select {
  width: 80%;
}
.detail .title {
  margin-bottom: 0.1rem;
  color: #05a4bf;
}
.detail .detailBox {
  display: flex;
  flex-wrap: wrap;
}
.detail .detailItem {
  width: 50%;
  margin-bottom: 0.1rem;
}
.history .title {
  margin: 0.1rem 0;
  color: #05a4bf;
}
.history .detailItem {
  margin-bottom: 0.1rem;
}
</style>